﻿using MyBBSWebApi.BLL.Interfaces;
using MyBBSWebApi.DAL;
using MyBBSWebApi.Models;
using System;
using System.Collections.Generic;

namespace MyBBSWebApi.BLL
{
    //public class UserBll      //不使用接口interface的时候，不需要继承接口的类
    //由于UserBll的类继承了IUserBll的接口，因此IUserBll中所有接口的抽象方法都必须在UserBll的类中实现，否则会报错
    public class UserBll : IUserBll
    {
        UserDal userDal = new UserDal();
        public List<Users> GetAll()
        {
            return userDal.GetAll().FindAll(m => !m.IsDelete);
        }
        public Users CheckLogin(string userno, string password)
        {
            List<Users> userlist = userDal.GetUserByusernoAndpassword(userno, password);
            if (userlist.Count <= 0)
            {
                return default;
            }
            else
            {
                Users user = userlist.Find(m => m.IsDelete == false);
                if (user == null) return default;
                else return user;
            }

            //return default;//返回该返回类型的默认值，自动推断返回值的类型
        }

        public string AddUser(int id, string userno, string username, int userlevel, string password)
        {
            UserDal userDal = new UserDal();
            

            int i = userDal.AddUser(id, userno, username, userlevel, password);
            if (i > 0)
            {
                return "数据插入成功";
            }
            else
            {
                return "数据插入失败";
            }
        }

        public string UpdateUser(int id, string userno, string username, int? userlevel, string password)
        {
            UserDal userDal = new UserDal();
            int i = userDal.UpdateUser(id, userno, username, userlevel, password);
            if (i > 0)
            {
                return "数据按需求更新成功";
            }
            else
            {
                return "数据按需求更新失败";
            }
        }

        public string RemoveUser(int id, string userno, string username)
        {
            UserDal userDal = new UserDal();
            int i = userDal.RemoveUser(id, userno, username);
            return $"{i}:数据删除成功";
        }

        public string RemoveID(int id)
        {
            UserDal userdal = new UserDal();
            int rows = userDal.RemoveID(id);
            if (rows > 0)
            {
                return "数据修改成功";
            }
            else
            {
                return "数据修改失败";
            }
        }
    }
}
